﻿using XYS.Service.Core.Models;
using XYS.Service.Core.Service;

namespace XYS.Core.Util.Helper
{
    /// <summary>
    /// 获取缓存Key
    /// </summary>
    public partial class CacheKeysHelper
    {
        /// <summary>
        /// 用户服务权限缓存
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="orgId"></param>
        /// <returns></returns>
        public static string UserActivateService(long userId, long orgId) => $"cache:user:activate:{userId}:{orgId}";

        /// <summary>
        /// 用户申请的token
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static string UserTokenKey(long userId) => $"cache:token:{userId}";

        /// <summary>
        /// 获取用户申请的token的key
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static string GetUserTokenKeys(long userId) => $"cache:token:{userId}:*";

        /// <summary>
        /// 用户token黑名单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static string UserTokenBlackKey(long userId) => $"cache:token:black:{userId}:{Guid.NewGuid()}";

        /// <summary>
        /// 用户token黑名单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static string GetUserTokenBlackKey(long userId) => $"cache:token:black:{userId}:*";



        /// <summary>
        /// 登录用户信息
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="orgId"></param>
        /// <returns></returns>
        public static string CacheCurrentUserKey(long userId, long orgId) => $"cache:loginOrgUser:{orgId}:{userId}";

        /// <summary>
        /// 删除用户信息缓存
        /// </summary>
        /// <param name="cache"></param>
        /// <param name="userId"></param>
        public static void ClearCacheCurrentUserByUser(ICache cache, long userId)
        {
            return;
            var keys = cache.RedisDB.Multiplexer.GetServer(cache.RedisDB.Multiplexer.GetEndPoints().First()).Keys(pattern: $"cache:loginOrgUser:*:{userId}").ToArray();
            foreach (var key in keys) { cache.Remove(key); }
        }

        /// <summary>
        /// 删除用户信息缓存
        /// </summary>
        /// <param name="cache"></param>
        /// <param name="orgid"></param>
        public static void ClearCacheCurrentUserByOrg(ICache cache, long orgid)
        {
            return;
            var keys = cache.RedisDB.Multiplexer.GetServer(cache.RedisDB.Multiplexer.GetEndPoints().First()).Keys(pattern: $"cache:loginOrgUser:{orgid}:*").ToArray();
            foreach (var key in keys) { cache.Remove(key); }
        }

        /// <summary>
        /// 删除用户信息缓存
        /// </summary>
        /// <param name="cache"></param>
        /// <param name="userId"></param>
        /// <param name="orgId"></param>
        public static void ClearCacheCurrentUser(ICache cache, long userId, long orgId)
        {
            return;
            cache.Remove(CacheCurrentUserKey(userId, orgId));
        }

        /// <summary>
        /// OSS 桶是否存在
        /// </summary>
        /// <param name="ossSetting"></param>
        /// <param name="bucketName"></param>
        /// <returns></returns>
        public static string GetBucketExistCache(OSSSetting ossSetting, string bucketName) => $"oss:{ossSetting.OSSType}:{bucketName}";

        /// <summary>
        /// 通过登录凭证Id 和客户IP 获取账号登录失败次数
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="clientIpAddress"></param>
        /// <returns></returns>
        public static string GetAccessFailedCountKey(long userId, string clientIpAddress) => $"Auth:LoginFailedCount:{clientIpAddress}:{userId}";


        /// <summary>
        /// 账号登录失败次数
        /// </summary>
        /// <returns></returns>
        public static string GetAccessFailedCountALLKey() => $"Auth:LoginFailedCount:*";


        /// <summary>
        /// 生成验证码缓存key
        /// </summary>
        /// <param name="operationType">发送通道类型</param>
        /// <param name="VerifyId">验证码Id</param>
        /// <returns></returns>
        public static string GenerateVerifyCodeCacheKey(int operationType, string VerifyId) => $"Verify:Key:{operationType}:{VerifyId}";

        /// <summary>
        /// 生成验证码次数缓存key
        /// </summary>
        /// <param name="operationType"></param>
        /// <param name="VerifyId"></param>
        /// <returns></returns>
        public static string GenerateVerifyCodeCountCacheKey(int operationType, string VerifyId) => $"Verify:Key:{operationType}:{VerifyId}-Count";

        /// <summary>
        /// 失效token
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static string InvalidityToken(string token) => $"InvalidityToken:{token.GetHashCode()}";
    }
}
